package gdut.imis.enums;

import java.util.Optional;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/7 11:17
 */
public enum Status {
    SUCCESS(0, "成功"),
    FAILED(1, "操作失败:{0}"),
    INTERNAL_SERVER_ERROR_ARGS(10000, "服务端异常:{0}"),
    REQUEST_PARAMS_NOT_VALID_ERROR(10001, "请求参赛[{0}]无效"),
    UNAUTHORIZED(401, "权限不足"),
    FORBIDDEN(403, "认证失败，请重新登录"),
    VALIDATE_FAILED(1001, "参数校验失败:{0}"),
    BUSINESS_EXCEPTION(1002, "业务异常{0}"),
    SQL_EXCEPTION(1003, "sql错误：{0}");

    private final int code;
    private final String msg;

    Status(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    /**
     * Retrieve Status enum entity by status code.
     */
    public static Optional<Status> findStatusBy(int code) {
        for (Status status : Status.values()) {
            if (code == status.getCode()) {
                return Optional.of(status);
            }
        }
        return Optional.empty();
    }
}
